package com.flx.dis.registry.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Fenglixiong
 * @Create 2022/4/12 16:55
 * @Description
 **/
public class ZkUsingForJava {

    public static void main(String[] args) throws Exception {

        setNodeAcl();

    }

    public static void createNode()throws Exception{

        ZooKeeper zk = new ZooKeeper("101.35.221.198:2181", 40000,null);
        List<ACL> aclList = new ArrayList<>();
        int perm1 = ZooDefs.Perms.READ|ZooDefs.Perms.WRITE;//cdwra
        int perm2 = ZooDefs.Perms.ADMIN;
        ACL acl1 = new ACL(perm1,new Id("world","anyone"));
        ACL acl2 = new ACL(perm2,new Id("world","anyone"));
        ACL acl3 = new ACL(perm2,new Id("ip","127.0.0.1"));
        aclList.add(acl1);
        aclList.add(acl2);
        aclList.add(acl3);
        String result = zk.create("/opt","hello".getBytes(),aclList, CreateMode.PERSISTENT);
        System.out.println(result);
    }

    /**
     * 获取数据
     */
    public static void getData() throws Exception {
        ZooKeeper zk = new ZooKeeper("101.35.221.198:2181", 40000,null);
        byte[] data = zk.getData("/feng", true, null);
        System.out.println(new String(data));
        Thread.sleep(Long.MAX_VALUE);
    }

    /**
     * 获取数据
     */
    public static void getDataDetail() throws Exception {
        ZooKeeper zk = new ZooKeeper("101.35.221.198:2181", 40000,null);
        zk.getData("/feng", false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int code, String path, Object ctx, byte[] result, Stat stat) {
                System.out.println("path = "+path+",code = "+code+",ctx = "+ctx+",result = "+new String(result)+",stat = "+stat);
            }
        }, "fenglixiong");
        Thread.sleep(Long.MAX_VALUE);
    }

    /**
     * 持续监听节点数据变化
     */
    public static void watchData()throws Exception{
        ZooKeeper zk = new ZooKeeper("101.35.221.198:2181", 40000, null);
        zk.getData("/feng", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("-----getData watcher");
                try {
                    byte[] data = zk.getData(event.getPath(), this, null);
                    System.out.println(event+"-----"+new String(data, StandardCharsets.UTF_8));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },null);
        Thread.sleep(Long.MAX_VALUE);
    }

    /**
     * 持续监听子节点数量变化
     */
    public static void watchNode()throws Exception{
        ZooKeeper zk = new ZooKeeper("101.35.221.198:2181", 40000, null);
        zk.getChildren("/feng", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("-----watchNode watcher");
                try {
                    List<String> childrens = zk.getChildren(event.getPath(), this, null);
                    System.out.println(event+"-----"+childrens);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },null);
        Thread.sleep(Long.MAX_VALUE);
    }

    public static void setNodeAcl()throws Exception{

        ZooKeeper zk = new ZooKeeper("101.35.221.198:2181", 40000, null);
        Stat stat = zk.setACL("/feng", Arrays.asList(new ACL(ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ, new Id("world", "anyone"))), 5);
        System.out.println(stat);

    }

}
